package com.ydm.myapp001.activity;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ydm.myapp001.R;
import com.ydm.myapp001.adapter.GoodsCategoryAdapter;
import com.ydm.myapp001.application.MyApplication;
import com.ydm.myapp001.bean.OneGoodsCategory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecyclerView001Activity extends AppCompatActivity {

    @BindView(R.id.rv_goodsCategory)
    RecyclerView rvGoodsCategory;

    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private GoodsCategoryAdapter goodsCategoryAdapter;
    private List<OneGoodsCategory> goodsCategoryArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view001);

        // 初始化View注入
        ButterKnife.bind(this);
        mContext = this;


        /*
         * 1.加载数据源--商品分类
         * */
        goodsCategoryArray = new ArrayList<OneGoodsCategory>();
        for(int i=1; i<=22; i++){
            OneGoodsCategory category = new OneGoodsCategory("商品分类"+i, "0", i==2?true:false);
            goodsCategoryArray.add(category);
        }

        //
        initGoodsCategoryRecyclerView();
    }

    private void initGoodsCategoryRecyclerView() {
        //多行多列
        GridLayoutManager glmGoodsCatefory = new GridLayoutManager(mContext, 3);
        rvGoodsCategory.setLayoutManager(glmGoodsCatefory);
        goodsCategoryAdapter = new GoodsCategoryAdapter(mContext, goodsCategoryArray, null);
        rvGoodsCategory.setAdapter(goodsCategoryAdapter);
        rvGoodsCategory.addItemDecoration(new GoodsCategoryGridItemDecoration(3, 10 * (int)((MyApplication)getApplication()).getScreenDensity()));


        /*
        //多行
        LinearLayoutManager llmGoodsCategory = new LinearLayoutManager(mContext,
                LinearLayoutManager.VERTICAL, false);
        rvGoodsCategory.setLayoutManager(llmGoodsCategory);
        goodsCategoryAdapter = new GoodsCategoryAdapter(mContext, goodsCategoryArray, null);
        rvGoodsCategory.setAdapter(goodsCategoryAdapter);
        rvGoodsCategory.addItemDecoration(new MyItemDecoration(mContext, 20 * (int)((MyApplication)getApplication()).getScreenDensity()));
        */
    }


    public class GoodsCategoryGridItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount; //列数
        private int spacing; //item之间的间距

        public GoodsCategoryGridItemDecoration(int spanCount, int spacing) {
            this.spanCount = spanCount;
            this.spacing = spacing;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position  （当前Item是第几个Item）
            int currentColumn = position % spanCount; // item column （当前Item位于第几列，0开始）
            int currentRow = position / spanCount; //item row （当前Item位于第几行，0开始）


            int left = currentColumn==0 ? spacing : 0;
            int right = spacing;
            int top = currentRow==0 ? spacing : 0;
            int bottom = spacing;
            outRect.set(left, top, right, bottom); //outRect中的left top right bottom是px值
        }
    }



    public class MyItemDecoration extends RecyclerView.ItemDecoration {
        private Context mContext;
        private int bottomGap;

        public MyItemDecoration(Context mContext, int bottomGap) {
            this.mContext = mContext;
            this.bottomGap = bottomGap;
        }

        /**
         * @param outRect 边界
         * @param view    recyclerView ItemView
         * @param parent  recyclerView
         * @param state   recycler 内部数据管理
         */
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            //设定底部边距
            outRect.set(0, 0, 0, bottomGap); //outRect中的left top right bottom是px值
        }
    }
}
